// pages/shop/shop-address.js
const app = getApp();
const util = require('../../utils/util.js');
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myAddressList:[]
  },
  changeAddress:function(e){
    let index = e.currentTarget.dataset.index,
      myAddressList = _this.data.myAddressList, 
    pages = getCurrentPages(),
    prevPage = pages[pages.length - 2];
    prevPage.setData({
      defAddress: myAddressList[index]
    });
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: 1
      })
    }
  },
  getAddressList: function () {
    let data = {};
    data.userCode = _this.data.userInfo.code
    app.getMassage(data, null, 'addressList', function (res) {
      if (res.status == 'success') {
        _this.setData({
          myAddressList: res.data,
        })
      }
    }, 'GET')
  },
  deleteAddress:function(e){
    let data = {},
      index = e.currentTarget.dataset.index;
    data.userCode = _this.data.userInfo.code;
    data.addressId = _this.data.myAddressList[index].id;
    app.getMassage(data, null,'addressDelete',function(res){
      if (res.data == 'SUCCESS') {
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        });
        _this.getAddressList();
      }
    })
  },
  defaultAddress:function(e){
    let index = e.currentTarget.dataset.index;
    // let url = _this.data.myAddressList[index].userCode + '/' + _this.data.myAddressList[index].id +'/def'
    let data = {};
    data.user_code = _this.data.myAddressList[index].userCode;
    data.address_id = _this.data.myAddressList[index].id;
    app.getMassage(data, null,'addressDef',function(res){
      if (res.status == 'success') {
        wx.showToast({
          title: '设置成功',
          icon: 'success'
        });
        _this.getAddressList();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    _this.setData({
      userInfo: app.globalData.xieYouUserInfo
    })
    _this.getAddressList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack: function (e) {
    app.goBack(e, _this.data.buttonClicked, _this);
  },
})